package daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import model.Experience;
import dao.ExperienceDAO;

@Repository("experienceDAOImpl")
public class ExperienceDAOImpl implements ExperienceDAO{
	
	@Override
	public ArrayList<Experience> getAllExperiences(){
		
		ArrayList<Experience> expList =new ArrayList<Experience>();
		Experience experience = null;
		Connection conn;
		ResultSet rs;
		
		try {
			conn = util.DatabaseUtils.dbConnection();
		
		String query = "SELECT text,userid,date FROM Experience WHERE id=?";
		PreparedStatement  statement = conn.prepareStatement(query);
		rs = statement.executeQuery();
		
		while(rs.next()){
			experience = new Experience();
			experience.setID(rs.getInt("id"));
			experience.setText(rs.getString("text"));
			experience.setCreatedUserID(rs.getInt("userid"));
			experience.setCreatedDate(rs.getDate("date"));
			expList.add(experience);
		}
		
		} catch (Exception e) {
			
		}

		return expList;
	}
	
	@Override
	public Experience getExperience(String Eid){
		Experience experience = null;
		Connection conn;
		ResultSet rs;
		
		try {
			conn = util.DatabaseUtils.dbConnection();
		
		String query = "SELECT text,userid,date FROM Experience WHERE id=?";
				
		PreparedStatement  statement = conn.prepareStatement(query);
		statement.setString(1,Eid);
		rs = statement.executeQuery();
		
		if(rs.next()){
			experience = new Experience();
			
			experience.setText(rs.getString("text"));
			experience.setCreatedUserID(rs.getInt("userid"));
			experience.setCreatedDate(rs.getDate("date"));		
		}
		
		} catch (Exception e) {
			
		}

		return experience;
	}
	
	@Override
	public Boolean saveExperience(Experience input){
		Connection conn;
		Boolean result = null;
		String text = input.getText();
		int userID = input.getID();
		try {
			conn = util.DatabaseUtils.dbConnection();
		
		String query = "INSERT INTO Experience (text,userid) VALUES(text,userID)";
				
		PreparedStatement  statement = conn.prepareStatement(query);
		statement.setString(1,input.getText());
		statement.setInt(2,input.getCreatedUserID());
		statement.setDate(3,null);
		result = statement.execute();
	
		} catch (Exception e) {
			
		}
		return result;
		
	}

	@Override
	public List<Experience> getPopulerExperience(int count) {
		List<Experience> experienceList = new ArrayList<Experience>();
		Experience experience = null;
		Connection conn;
		ResultSet rs;
		
		try {
			conn = util.DatabaseUtils.dbConnection();
		
		String query = "SELECT text,userid,date FROM Experience LIMIT ?";
				
		PreparedStatement  statement = conn.prepareStatement(query);
		statement.setInt(1,count);
		rs = statement.executeQuery();
		
		while(rs.next()){
			experience = new Experience();
			experience.setText(rs.getString("text"));
			experience.setCreatedUserID(rs.getInt("userid"));
			experience.setCreatedDate(rs.getDate("date"));	
			
			experienceList.add(experience);
		}
		
		} catch (Exception e) {
			
		}
		return experienceList;
	}


}
